<template>
  <div class="projects-page">
    <div class="page-header">
      <div class="header-content">
        <div>
          <h1 class="page-title">项目管理</h1>
          <p class="page-subtitle">管理项目和关联的文档资料</p>
        </div>
        <div class="header-actions">
          <el-button type="primary">
            <el-icon><Plus /></el-icon>
            新建项目
          </el-button>
        </div>
      </div>
    </div>

    <div class="projects-content">
      <div class="projects-grid">
        <div
          v-for="project in projectStore.projects"
          :key="project.id"
          class="project-card"
          @click="viewProject(project)"
        >
          <div class="project-header">
            <div class="project-color" :style="{ background: project.color }"></div>
            <div class="project-title">{{ project.name }}</div>
            <el-tag :type="getStatusType(project.status)">{{ getStatusText(project.status) }}</el-tag>
          </div>
          
          <div class="project-description">{{ project.description }}</div>
          
          <div class="project-stats">
            <div class="stat-item">
              <div class="stat-value">{{ getDocumentCount(project) }}</div>
              <div class="stat-label">文档</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ project.memberCount }}</div>
              <div class="stat-label">成员</div>
            </div>
            <div class="stat-item">
              <div class="stat-value">{{ project.progress }}%</div>
              <div class="stat-label">进度</div>
            </div>
          </div>
          
          <div class="project-progress">
            <el-progress 
              :percentage="project.progress" 
              :stroke-width="6" 
              :show-text="false"
              :color="project.color"
            />
          </div>
          
          <div class="project-footer">
            <span class="project-manager">{{ project.manager }}</span>
            <span class="project-time">{{ formatTime(project.updateTime) }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useProjectStore } from '@/store/project'
import { Plus } from '@element-plus/icons-vue'

const router = useRouter()
const projectStore = useProjectStore()

const getStatusType = (status) => {
  const types = {
    'active': 'success',
    'planning': 'warning',
    'completed': 'info',
    'paused': 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    'active': '进行中',
    'planning': '计划中',
    'completed': '已完成',
    'paused': '已暂停'
  }
  return texts[status] || status
}

const getDocumentCount = (project) => {
  return project.documents ? project.documents.length : 0
}

const formatTime = (timeStr) => {
  const time = new Date(timeStr)
  const now = new Date()
  const diff = now - time
  const days = Math.floor(diff / (24 * 60 * 60 * 1000))
  
  if (days < 1) return '今天'
  if (days < 7) return `${days}天前`
  return time.toLocaleDateString()
}

const viewProject = (project) => {
  router.push(`/documents?project=${project.id}`)
}
</script>

<style lang="scss" scoped>
.projects-page {
  padding: 20px;
}

.page-header {
  margin-bottom: 24px;
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    
    .page-title {
      font-size: 28px;
      font-weight: 600;
      color: var(--el-text-color-primary);
      margin-bottom: 4px;
    }
    
    .page-subtitle {
      color: var(--el-text-color-secondary);
      font-size: 14px;
    }
  }
}

.projects-content {
  .projects-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 20px;
  }
}

.project-card {
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-light);
  border-radius: 12px;
  padding: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-base);
    border-color: var(--el-color-primary);
  }
  
  .project-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    
    .project-color {
      width: 16px;
      height: 16px;
      border-radius: 4px;
      flex-shrink: 0;
    }
    
    .project-title {
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
      flex: 1;
    }
  }
  
  .project-description {
    color: var(--el-text-color-secondary);
    font-size: 14px;
    line-height: 1.5;
    margin-bottom: 20px;
    @extend .text-ellipsis-2;
  }
  
  .project-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 16px;
    
    .stat-item {
      text-align: center;
      padding: 12px;
      background: var(--el-fill-color-lighter);
      border-radius: 8px;
      
      .stat-value {
        font-size: 20px;
        font-weight: 600;
        color: var(--el-color-primary);
        margin-bottom: 4px;
      }
      
      .stat-label {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }
    }
  }
  
  .project-progress {
    margin-bottom: 16px;
  }
  
  .project-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}
</style>